package com.sx.manage.controller.company;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sx.common.util.ValidateUtil;
import com.sx.common.util.http.constants.PaperType;
import com.sx.common.util.json.JackJsonUtil;
import com.sx.manage.controller.base.MBaseController;
import com.sx.manage.service.company.IMCompanyGroupInfoService;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.model.manage.mybatis.form.consumer.MCompanyForm;
import com.sx.model.manage.mybatis.vo.bank.MDropDownVO;
import com.sx.model.manage.mybatis.vo.company.MCompanyVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultDropDownListVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;
/**
 * 集团
 * @author zwm
 *
 */
@Controller
@RequestMapping("/companyGroup")
public class MCompanyGroupController extends MBaseController{
	
	private Logger logger = LoggerFactory.getLogger(MCompanyGroupController.class);
	
	@Resource(name="companyGroupInfoServiceImpl")
	private IMCompanyGroupInfoService companyGroupInfoServiceImpl;
	
	/**
	 * 添加集团公司视图页
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping(value="query_code")
	@ResponseBody
	public ViewAndModel add_viewCompany()throws ServletException, IOException{
		ViewAndModel viewAndModel = new ViewAndModel(request,cache);
		Long company_code = this.companyGroupInfoServiceImpl.getCompanyCode();
		logger.info("获取公司编码{}",company_code);
		MCompanyVO mCompanyVO = new MCompanyVO();
		mCompanyVO.setCompany_code( company_code );
		viewAndModel.addObject("mCompanyVO", mCompanyVO);
		viewAndModel.setViewName("company/group/companyedit");
		return viewAndModel;
	}
	/**
	 * 编辑界面
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/query_view_company/{id}")
	@ResponseBody
	public ViewAndModel query_view_company(MCompanyForm form)throws ServletException, IOException{
		logger.debug("添加系统操作日志：MCompanyController===search_SysIntroduce==formJson="+JackJsonUtil.objToJson( form ));
		ViewAndModel viewAndModel = new ViewAndModel(request,cache);
		viewAndModel.setViewName("company/group/companyedit");
		ResultObjectVO<MCompanyVO>  resultObjectVO = companyGroupInfoServiceImpl.search_byIds(form);
		MCompanyVO mCompanyVO = resultObjectVO.getObjVo();
		viewAndModel.addObject("mCompanyVO", mCompanyVO);
		return viewAndModel;
	}
	/**
	 * 添加集团公司数据信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/company_group_save")
	@ResponseBody
	public ResultBooleanVO company_group_save(MCompanyForm form)throws ServletException, IOException{
		
		if( StringUtils.isNotBlank(form.getEmail()) && !ValidateUtil.isEmailComOrCn( form.getEmail())){
			return new ResultBooleanVO(false,null,"邮箱格式错误");
		}
		if( PaperType.IDENTITYCARD.equals( form.getPapers_type()) && !ValidateUtil.isLawNo( form.getPapers_num()) ){
			return new ResultBooleanVO(false,null,"身份证格式不正确");
		}
		if( null==form.getId()){
			return companyGroupInfoServiceImpl.company_save(form, this.loginConsumer());
		}else{
			return companyGroupInfoServiceImpl.company_edit(form, this.loginConsumer());
		}
	
	}
	/**
	 * 查询集团公司页
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/search_company_group_view")
	public ViewAndModel search_company_group_view()throws ServletException, IOException{
		ViewAndModel viewAndModel = new ViewAndModel(request,cache);
		viewAndModel.setViewName("company/group/commanage");
		return viewAndModel;
	}
	
	/**
	 * 获取公司分页数据
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/search_page_company")
	@ResponseBody
	public ResultListVO<MCompanyVO> search_company_group(MCompanyForm form)throws ServletException, IOException{
		if("".equals(form.getStatus())){
			form.setStatus(null);
		}
		return companyGroupInfoServiceImpl.get_company_page(form);
	}
	
	/**
	 * 编辑集团公司数据信息
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/company_group_edit")
	@ResponseBody
	public ResultBooleanVO company_group_edit(MCompanyForm form)throws ServletException, IOException{
		
		if( StringUtils.isNotBlank(form.getEmail()) && !ValidateUtil.isEmailComOrCn( form.getEmail())){
			return new ResultBooleanVO(false,null,"邮箱格式错误");
		}
		if( PaperType.IDENTITYCARD.equals( form.getPapers_type()) && !ValidateUtil.isLawNo( form.getPapers_num()) ){
			return new ResultBooleanVO(false,null,"身份证格式不正确");
		}
		
		return companyGroupInfoServiceImpl.company_edit(form, this.loginConsumer());
	}
	
	/**
	 * 集团公司管理-下拉公司列表
	 * @param request
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/query_company_group_branch")
	@ResponseBody
	public ResultDropDownListVO<MDropDownVO> ajax_company_info(HttpServletRequest request)throws ServletException, IOException{
		String str = request.getQueryString();
		String q = str.split("&")[0];
		MCompanyForm form = new MCompanyForm();
		if (null != q) {
			String company_name = new String(q.split("=")[1].getBytes("iso8859_1"));
			form.setCompany_name(java.net.URLDecoder.decode(company_name,"utf-8"));
		}
		return companyGroupInfoServiceImpl.query_company(form);
	}
}
